import MarkdownIt from "markdown-it";

const baseURL = `http://localhost:3000`
export function fetchData(options){
	return new Promise((resolve,reject)=>{
		uni.request({
			...options,
			method:options.method||'GET',
			url:`${baseURL}${options.url}`,
			success:(response)=>{
				if(response.statusCode === 200){
					resolve(response.data);
				}else{
					reject(new Error('获取数据失败'));
				}
			},
			fail:(error)=>{
				console.log(error);
				reject(error);
			}
		})
	});
}
/**
 * selector 类似于 CSS 的选择器，但仅支持ID选择器和类选择器
 * selectorQuery 选择器查询 的方式 或者select查询 匹配第一项 selectAll查询 匹配的所有的项
 * properties 查询 节点的属性 NodesRef 属性值
 */
export const selectFromQuery = (selector,selectorQuery,properties)=>{
	return new Promise((resolve)=>{
		uni.createSelectorQuery()
		[selectorQuery](selector)[properties]()
		.exec(result=>resolve(result));
	});
}
export const markdown = new MarkdownIt();
//我们会把学习的历史保存在本地存储中，这是保存的key
export const STUDY_HISTORY= 'STUDY_HISTORY';
export const formatTime = (time)=>{
  const date = new Date(time);
  return `${date.getFullYear()}年${date.getMonth()+1}月${date.getDate()}日${date.getHours()}时${date.getMinutes()}分${date.getSeconds()}秒`;
}